package com.ruoyi.asset.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.asset.domain.AstTaskUser;
import com.ruoyi.asset.domain.AstTaskUserList;
import com.ruoyi.asset.domain.AstToolList;
import com.ruoyi.asset.service.IAstTaskUserService;
import com.ruoyi.asset.service.IAstToolListService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.asset.domain.AstTask;
import com.ruoyi.asset.service.IAstTaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务管理Controller
 * 
 * @author xunzhe
 * @date 2024-06-18
 */
@Controller
@RequestMapping("/asset/astTask")
public class AstTaskController extends BaseController
{
    private String prefix = "asset/astTask";

    @Autowired
    private IAstTaskService astTaskService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IAstTaskUserService taskUserService;

    @Autowired
    private IAstToolListService toolListService;

    @RequiresPermissions("asset:astTask:view")
    @GetMapping()
    public String astTask()
    {
        return prefix + "/astTask";
    }

    /**
     * 查询任务管理列表
     */
    @RequiresPermissions("asset:astTask:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AstTask astTask)
    {
        startPage();
        List<AstTask> list = astTaskService.selectAstTaskList(astTask);
        return getDataTable(list);
    }

    /**
     * 导出任务管理列表
     */
    @RequiresPermissions("asset:astTask:export")
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AstTask astTask)
    {
        List<AstTask> list = astTaskService.selectAstTaskList(astTask);
        ExcelUtil<AstTask> util = new ExcelUtil<AstTask>(AstTask.class);
        return util.exportExcel(list, "任务管理数据");
    }

    /**
     * 新增任务管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<AstToolList> toolLists = toolListService.selectAstToolListList(new AstToolList());
        mmap.put("toolLists",toolLists);
        return prefix + "/add";
    }

    /**
     * 新增保存任务管理
     */
    @RequiresPermissions("asset:astTask:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AstTask astTask)
    {
        return toAjax(astTaskService.insertAstTask(astTask));
    }

    /**
     * 修改任务管理
     */
    @RequiresPermissions("asset:astTask:edit")
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        AstTask astTask = astTaskService.selectAstTaskByTaskId(taskId);
        List<AstToolList> toolLists = toolListService.selectAstToolListList(new AstToolList());
        if(astTask.getToolListIds()!=null && !astTask.getToolListIds().equals("")){
            String[] split = astTask.getToolListIds().split(",");
            for(String str : split){
                for(AstToolList toolList : toolLists){
                    if(toolList.getToolId().longValue()==Long.parseLong(str)){
                        toolList.setFlag(true);
                        break;
                    }
                }
            }
        }
        mmap.put("astTask", astTask);
        mmap.put("toolLists",toolLists);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务管理
     */
    @RequiresPermissions("asset:astTask:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AstTask astTask)
    {
        return toAjax(astTaskService.updateAstTask(astTask));
    }

    /**
     * 删除任务管理
     */
    @RequiresPermissions("asset:astTask:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(astTaskService.deleteAstTaskByTaskIds(ids));
    }

    @RequiresPermissions("asset:astTask:edit")
    @GetMapping("/assignmentUser/{taskId}")
    public String assignmentUser(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("taskId", taskId);
        return prefix + "/assignmentUser";
    }

    @PostMapping("/assignmentUser")
    @ResponseBody
    public TableDataInfo assignmentUser(SysUser sysUser)
    {
        startPage();
        List<AstTaskUser> list = taskUserService.selectTaskUserList(sysUser);
        return getDataTable(list);
    }

    @PostMapping("/assignment")
    @ResponseBody
    public AjaxResult assignment(Long taskId, String userIds)
    {
        return toAjax(taskUserService.assignment(taskId,userIds));
    }

    @RequiresPermissions("asset:astTask:edit")
    @GetMapping("/selectUser/{taskId}")
    public String selectUser(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("taskId", taskId);
        return prefix + "/selectUser";
    }

    @PostMapping("/selectUser")
    @ResponseBody
    public TableDataInfo selectUser(SysUser sysUser)
    {
        startPage();
        List<SysUser> list = userService.selectCanTaskUserList(sysUser);
        return getDataTable(list);
    }

    @PostMapping( "/deleteUser")
    @ResponseBody
    public AjaxResult deleteUser(Long taskId,String userIds)
    {
        return toAjax(taskUserService.deleteUser(taskId,userIds));
    }

//    @GetMapping("/updateDescribe/{assignmentId}")
//    public String updateDescribe(@PathVariable("assignmentId") Long assignmentId, ModelMap mmap)
//    {
//        AstTaskUser taskUser = taskUserService.selectAstTaskUserByAssignmentId(assignmentId);
//        mmap.put("taskUser", taskUser);
//        return prefix + "/updateDescribe";
//    }
//
//    @PostMapping("/updateDescribe")
//    @ResponseBody
//    public AjaxResult updateDescribeSave(AstTaskUser taskUser)
//    {
//        return toAjax(taskUserService.updateAstTaskUser(taskUser));
//    }

    @GetMapping("/taskUserList/{taskId}")
    public String taskUserList(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("taskId",taskId);
        return prefix + "/astTaskUserList";
    }

    @PostMapping("/taskUserList")
    @ResponseBody
    public TableDataInfo taskUserList(AstTaskUserList astTaskUserList)
    {
        startPage();
        List<AstTaskUserList> list = astTaskService.selectAstTaskUserList(astTaskUserList);
        return getDataTable(list);
    }

    @GetMapping("/taskUserListDetail/{listId}")
    public String taskUserListDetail(@PathVariable("listId") Long listId, ModelMap mmap)
    {
        AstTaskUserList taskUserList = astTaskService.selectAstTaskUserListById(listId);
        mmap.put("taskUserList",taskUserList);
        return prefix + "/taskUserListDetail";
    }


    //驳回站点完成的任务
    @PostMapping("/bohuiListStatus")
    @ResponseBody
    public AjaxResult bohuiListStatus(Long listId,String listStatus)
    {
        return toAjax(astTaskService.bohuiListStatus(listId, listStatus));
    }



}
